package com.igeek.boot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.igeek.boot.entity.WmsLog;
import com.igeek.boot.mapper.WmsLogMapper;
import com.igeek.boot.service.IWmsLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.igeek.boot.vo.PageQueryVO;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jwj
 * @since 2024-01-08
 */
@Service
public class WmsLogServiceImpl extends ServiceImpl<WmsLogMapper, WmsLog> implements IWmsLogService {

    //按照module和operation和操作人进行查询日志
    @Override
    public Page<WmsLog> findPage(PageQueryVO queryVO) {
        //封装分页条件
        Page<WmsLog> wmsLogPage = new Page<>(queryVO.getCurrentPage(), queryVO.getPageSize());

        //封装查询条件
        LambdaQueryWrapper<WmsLog> lqw = new LambdaQueryWrapper<WmsLog>().like(StringUtils.hasLength(queryVO.getQueryString()), WmsLog::getModule, queryVO.getQueryString())
                .or()
                .like(StringUtils.hasLength(queryVO.getQueryString()), WmsLog::getOperation, queryVO.getQueryString())
                .or()
                .like(StringUtils.hasLength(queryVO.getQueryString()), WmsLog::getOperateUser, queryVO.getQueryString());

        //执行查询
        Page<WmsLog> page = baseMapper.selectPage(wmsLogPage, lqw);
        return page;
    }

    //根据当天时间 查询当天的所有入库订单信息
    public List<WmsLog> findTodayInStock(){
        //获取当前时间  前一天两点的时间  和今天两点的时间
        LocalDateTime beforeDay = LocalDate.now().minusDays(1).atTime(2, 0);
        LocalDateTime today = LocalDate.now().atTime(2, 0);
        //进行查询所有的入库订单信息
        LambdaQueryWrapper<WmsLog> lqw = new LambdaQueryWrapper<WmsLog>().between(WmsLog::getOperateTime, beforeDay, today)
                .eq(WmsLog::getOperation, "执行入库操作");
        return baseMapper.selectList(lqw);
    }

    //根据当天时间 查询当天的所有出库订单信息
    public List<WmsLog> findTodayOutStock(){
        //获取当前时间  前一天两点的时间  和今天两点的时间
        LocalDateTime beforeDay = LocalDate.now().minusDays(1).atTime(2, 0);
        LocalDateTime today = LocalDate.now().atTime(2, 0);
        //进行查询所有的出库订单信息
        LambdaQueryWrapper<WmsLog> lqw =
                new LambdaQueryWrapper<WmsLog>().between(WmsLog::getOperateTime,beforeDay, today)
                        .eq(WmsLog::getOperation, "进行出库操作");
        return baseMapper.selectList(lqw);
    }
}
